import Vue from 'vue'
import Vuex from 'vuex'
import { nowPlayingListData } from "@/api/api";
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0,
    token:'',
    usermobile:'',
    is_show2: true,
    city: localStorage.getItem('city') || '上海',
    cityid: localStorage.getItem('cityid') || 310100,
    storenowplayinglist: []
  },
  // vue变异器 数据的突变  改变 
  mutations: {
    //  this.$store.commit('add',1)
    add(state, step) {
      console.log('来到了  变异器 add里面了')
      state.count = state.count + step
    },
    addcityinfo(state, step) {
      console.log('来到了  变异器 add里面了')
      state.city = step.name
      localStorage.setItem('city', step.name)
      state.cityid = step.cityId
      localStorage.setItem('cityid', step.cityId)

    },
    addnowplaying(state, step) {
      state.storenowplayinglist = step
    },
    // usermobile全局数据保存mutation 
    usermobile(state,step) {
      console.log('usermobile') 
      state.usermobile = step 
    },
    tokenadd(state,step) {
      console.log('tokenadd mutation')
      state.token = step 
    }
  },
  // 异步数据改变放在这里 

  actions: {
    atime1(context, step) {
      console.log('来到了 actions 异步改变数据里')

      setTimeout(() => {
        context.commit('add', step)
      }, 1000);
    },
    // 请求 正在热映的页面 
    atimenowplayingdata(context, step) {
      console.log('来到了 actions atimenowplayingdata 异步改变数据里')
      nowPlayingListData(step).then((res) => {
        console.log('res', res)
        context.commit('addnowplaying', res.data.data.films)
      })
    }
  },
  // 工作中大厂的常见用法
  getters: {
    // this.$store.getters.getCount
    getCount: function (state) {
      return  state.count + '$$$$' 
    }
  }


})
